package com.black.utils.command;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.black.utils.commons.display.IOPrintUtil.print;

/**
 * @author ylx
 */
@Slf4j
@Getter
@Setter
public class LinuxLocalCmdExecutor extends CmdExecutor
{

    @Override
    public String pwd()
    {
        List<String> result = execute("pwd");
        if (!result.isEmpty())
        {
            return result.get(0);
        }
        return "";
    }

    @Override
    public List<String> ls()
    {
        return execute("ls");
    }

    @Override
    public List<String> ls(String path)
    {
        return execute("ls " + path);
    }

    @Override
    public List<String> ll()
    {
        return execute("ll");
    }

    @Override
    public List<String> ll(String path)
    {
        return execute("ll " + path);
    }

    @Override
    public boolean cd(String path)
    {
        return exec("cd " + path);
    }

    @Override
    public boolean mv(String source, String target)
    {
        return exec("mv " + source + " " + target);
    }

    @Override
    public boolean rename(String source, String target)
    {
        return mv(source, target);
    }

    @Override
    public boolean rm(String path)
    {
        return exec("rm -rf " + path);
    }

    @Override
    public boolean rmdir(String path)
    {
        return rm(path);
    }

    @Override
    public boolean mkdir(String path)
    {
        return exec("mkdir " + path);
    }

    @Override
    public boolean mkdirs(String path)
    {
        return exec("mkdir -p " + path);
    }

    @Override
    public String ip()
    {
        return execute("ifconfig").stream().collect(Collectors.joining(System.lineSeparator()));
    }

    @Override
    public boolean exec(String... cmd)
    {
        return exec(String.join(" && ", cmd));
    }

    @Override
    public boolean exec(String cmd)
    {
        // 构造需要执行的脚本路径
        try
        {
            // 设置命令
            String[] command = {BASH_SHELL, PARAM_C, cmd};

            // 使用 ProcessBuilder 执行命令
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            // 合并错误输出
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            // 读取脚本输出
            if (isPrintOutput)
            {
                print(process.getInputStream());
            }
            // 等待脚本执行完成
            int exitCode = process.waitFor();
            log.info("cmd exec end exit code：{}", exitCode);
            return exitCode == 0;
        }
        catch (Exception e)
        {
            log.error("execute cmd error", e);
            return false;
        }
    }

    @Override
    public List<String> execute(String cmd)
    {
        // 构造需要执行的脚本路径
        try
        {
            // 设置命令
            String[] command = {BASH_SHELL, PARAM_C, cmd};

            // 使用 ProcessBuilder 执行命令
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            // 合并错误输出
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            // 读取脚本输出
            List<String> result = print(process.getInputStream());
            // 等待脚本执行完成
            int exitCode = process.waitFor();
            log.info("cmd exec end exit code：{}", exitCode);
            return result;
        }
        catch (Exception e)
        {
            log.error("execute cmd error", e);
            return new ArrayList<>();
        }
    }
}
